<?php
ini_set('max_execution_time', 900);
set_time_limit(900);
ini_set("display_errors",1);
error_reporting(E_ALL);

require_once('Classes/PHPExcel/IOFactory.php');
$file_name = 'havokaren.xlsx';
$dir = dirname(__FILE__) . "/excelfile/";

$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("Van Cam")
						 ->setLastModifiedBy("Van Cam")
						 ->setTitle("Havokaren products")
						 ->setSubject("Havokaren")
						 ->setDescription("All Havokaren products from Terminal")
						 ->setKeywords("products");
// Create connection
require_once('simplesql.class.php5');

$ini = parse_ini_file("web.ini");
while(list($key,$value) = each($ini)){
	if($key == 'hostName'){
		$hostName = $value;
	}
	else if($key == 'userName'){
		$userName = $value;
	}
	else if($key == 'password'){
		$password = $value;
	}
	else if($key == 'dbName'){
		$dbName = $value;
	}
}


$db = new simplesql($hostName,$userName,$password,$dbName);
$db->query("SET NAMES utf8");
echo '<pre>';
$rows = $db->fetch_query("SELECT f.manufacturer_id, e.category_id, a.product_id, f.name supplier, e.name cat_name, a.sku, b.name, a.price  FROM 
(((((`product` a LEFT JOIN `product_description` b ON a.product_id = b.product_id)
LEFT JOIN `product_to_category` c ON a.product_id = c.product_id) LEFT JOIN `category` d ON c.category_id=d.category_id) 
LEFT JOIN `category_description` e ON d.category_id=e.category_id)
LEFT JOIN `manufacturer` f ON a.manufacturer_id=f.manufacturer_id)
WHERE a.date_added >= '2013-12-20' AND a.date_added <= '2014-03-17'
GROUP BY a.manufacturer_id, e.category_id, a.product_id", FETCH_ASSOC);

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue("A1", "SupplierName")
            ->setCellValue("B1", "ItemGroupName")
            ->setCellValue("C1", "Kparam1")
            ->setCellValue("D1", "Kparam2")
			->setCellValue("E1", "SalesPriceEach");
            
$i=2;
var_dump($rows);
foreach($rows as $key=>$value){
	$myindex 		= 'A';
	
	$product_id		= $value->product_id;
	$supplier 		= $value->supplier;
	$cat_name		= $value->cat_name;
	$sku 			= $value->sku;
	$name			= $value->name;
	$price 			= $value->price;
	
	echo $product_id . "<br />";
	
	
	$objPHPExcel->setActiveSheetIndex(0)
		->setCellValue($myindex.$i, $supplier)
		->setCellValue(++$myindex.$i, $cat_name)
		->setCellValue(++$myindex.$i, $sku)
		->setCellValue(++$myindex.$i, $name)
		->setCellValue(++$myindex.$i, $price);
		
		
	$i++;
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($dir.$file_name);			



echo '</pre>';
?> 